package com.huahai.security.core.validate.code;

import com.huahai.security.core.properties.SecurityProperties;
import com.huahai.security.core.validate.code.image.ImageCodeGenerator;
import com.huahai.security.core.validate.code.mobile.sms.DefaultSmsCodeSender;
import com.huahai.security.core.validate.code.mobile.sms.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Jun
 * @Date: 2020/7/17
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 若容器中没有自定义的图片验证码生成器，则采用默认的
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name="imageValidateCodeGenerator")
    public ValidateCodeGenerator imageValidateCodeGenerator(){
        ImageCodeGenerator imageCodeGenerator = new ImageCodeGenerator();
        imageCodeGenerator.setSecurityProperties(securityProperties);
        return imageCodeGenerator;
    }

    /**
     * 若程序没有声明 短信验证码发送器  则采用默认的
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeSender(){
        return new DefaultSmsCodeSender();
    }
}
